package com.sh.dc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.dc.common.pojo.query.ParamPageQuery;
import com.sh.dc.entity.Param;
import com.sh.dc.mapper.ParamMapper;
import com.sh.dc.service.IParamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 参数 服务实现类
 * </p>
 *
 * @author zhenlan
 * @since 2024-07-11
 */
@Service
public class ParamServiceImpl extends ServiceImpl<ParamMapper, Param> implements IParamService {

    @Autowired
    private ParamMapper paramMapper;

    @Override
    public IPage<Param> queryParamPage(ParamPageQuery query) {
        Page<Param> page = new Page<>(query.getPageNum(), query.getPageSize());
        QueryWrapper<Param> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getName())) {
            queryWrapper.like("name", query.getName());
        }
        if (StringUtils.isNotBlank(query.getValueType())) {
            queryWrapper.eq("value_type", query.getValueType());
        }
        if (query.getRequired() != null) {
            queryWrapper.eq("required", query.getRequired());
        }
        if (query.getStartTime() != null && query.getEndTime() != null) {
            queryWrapper.between("update_time", query.getStartTime(), query.getEndTime());
        }
        return paramMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean paramNameExisted(String name) {
        Param param = paramMapper.selectOne(new QueryWrapper<Param>().eq("name", name));
        return param != null;
    }
}
